﻿using RightControl.Common;
using RightControl.IService;
using RightControl.Model;
using RightControl.WebApp.Areas.Admin.Controllers;
using System;
using System.Web.Mvc;
using AlibabaCollector;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.IO;
using RightControl.WebApp.Models;

namespace RightControl.WebApp.Areas.DataSrv.Controllers {
    /// <summary>
    /// 运费模板
    /// </summary>
    public class ShipController : BaseController {
        public static IExportFieldsService exportFieldsService { get; set; }
        public static IBaseCategoryService baseCategoryService { get; set; }
        public static IShipService shipService { get; set; }
        public static IPlatformAuthService platformAuthSvr { get; set; }

        public  ActionResult Index(int? id) {
            var exportFields = exportFieldsService.GetExportFields("NZ", 1);
            ViewData["TableHead"] = exportFields;
            ViewData["BaseCategory"] = baseCategoryService.GetAllCategory();
            base.Initialize(id);
            return View();
        }

        public ActionResult AmazonAdd() {
            return View();
        }

        public ActionResult AmazonIndex(int? id) {
            base.Initialize(id);
            return View();
        }
        
        [HttpPost]
        public ActionResult AmazonAdd(ShipTemplate model) {
            Boolean isOk = false;
            if (model!=null) {
                model.PlatForm = (int)PlatFormEnum.Amazon;
                model.CreateBy = Operator.UserId;
                model.UpdateBy = Operator.UserId;
                model.CreateOn = DateTime.Now;
                isOk = shipService.InsertModel(model) > 0;
            }
            var result = isOk ? SuccessTip() : ErrorTip();
            return Json(result);
        }

        public ActionResult Edit(int? Id) {
            int templateId = Id ?? 0;
            var model = shipService.ReadModel(templateId);
            return View(model);
        }

        public ActionResult TemplateList(PageInfo pageInfo, ShipTemplate filter) {         
            filter.CreateBy = Operator.UserId;
            var shipList = shipService.GetListByFilter(filter, pageInfo);
            return Json(shipList, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult Delete(int id, int platform) {
            ShipTemplate auth = new ShipTemplate();
            auth.Id = id;
            auth.PlatForm = platform;
            auth.CreateBy = Operator.UserId;
            var result = shipService.Delete(auth) ? SuccessTip() : ErrorTip();
            return Json(result);
        }

        public ActionResult DhIndex(int? id) {
            base.Initialize(id);
            return View();
        }

        public ActionResult DhAdd() {
            ViewData["AuthShops"] = platformAuthSvr.GetListByFilter(new PlatformAuth() { UserId = Operator.UserId, PlatForm = (int)PlatFormEnum.DhGate }, new PageInfo(1, 10));

            return View();
        }

        [HttpPost]
        public ActionResult DhAdd(ShipTemplate model) {
            Boolean isOk = false;
            if (model != null) {
                if (model.FstDiscount > model.SecDiscount) {
                    return Json(ErrorTip("一档折扣不能大于二挡折扣!"));
                }
                model.PlatForm = (int)PlatFormEnum.DhGate;
                model.CreateBy = Operator.UserId;
                model.UpdateBy = Operator.UserId;
                model.CreateOn = DateTime.Now;
                isOk = shipService.InsertModel(model) > 0;
            }
            var result = isOk ? SuccessTip() : ErrorTip();
            return Json(result);
        }

        public ActionResult GetDhUserTemplates(int authId) {
            var pAuth = platformAuthSvr.GetAuthByUserId(new PlatformAuth() { Id = authId, UserId = Operator.UserId, PlatForm = (int)PlatFormEnum.DhGate }, new PageInfo(1, 10));
            if (pAuth != null) {
                TemplateContent templateContent = new TemplateContent();
            

                templateContent.CategoryList = baseCategoryService.GetAllCategory();

                return Content(SerializerHelper.JsonSerialized<TemplateContent>(templateContent));
            }
            else {
                return Json(ErrorTip(), JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult GetSizeTemplates(string catePubId) {
            var pAuth = platformAuthSvr.GetAuthByUserId(new PlatformAuth() { UserId = Operator.UserId, PlatForm = (int)PlatFormEnum.DhGate }, new PageInfo(1, 10));
            if (pAuth != null) {
              
                return Content(SerializerHelper.JsonSerialized<dynamic>(""));
            }
            else {
                return Json(ErrorTip(), JsonRequestBehavior.AllowGet);
            }
        }        
    }
}